package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 光明网解析
 * @author Persh
 *
 */
public class GmwNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
	"http://.*.gmw.cn/html/article/[0-9]*/[0-9]*/[0-9]*.shtml",
		"http://.*.gmw.cn/.*[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*.htm",
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title=JSoupUtils.select(doc, "#articleTitle","#Content_Title","#ArticleTitle","h1.picContentHeading",".news_title");
			Elements select=doc.select("#contentMain");
			if(select!=null){
				select.select("div#displaypagenum").remove();
				select.select("[style=display:none]").remove();
				content=HtmlCleaner.getContentHtml(url,select);
			}
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("#ArticleContent"));
				content=content.replace("正在加载中... ", "");
			}
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("#news_content"));
			}

			String tempdate=doc.select("#pubTime").text();
			if(tempdate==null || tempdate.trim().length()==0){
				tempdate=doc.select("#ArticleSourceAuthor").text();
			}
			date=DateUtils.matchDate(tempdate);
			if (date==null){
				date=JSoupUtils.matchDate(doc, "发布时间");
			}

			/**
			 * 解析用于更新的地址
			 */
			String updateUrl=null;
			String[] split=url.split("/");
			if(split.length>0){
				String content_=split[split.length-1];
				if(content_.indexOf("content_")!=-1){
					String domain=split[2];
					String dizhi1=split[3];
					String dizhi2=split[4];
					String articleid=content_.substring(content_.indexOf("_")+1,content_.indexOf("."));
					updateUrl ="http://" + domain + "/pinglun/" + dizhi1 + "/" + dizhi2 + "/pinglun_" + articleid+".html";
				}
			}
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setDate(date);

			String author=doc.select("div#contentMsg span#source").text();
			if(author!=null){
				author=author.replace("来源：", "");
			}
			news.setAuthor(author);


		return news;
	}

	public static void main(String[] args) {
		GmwNewsAnalyse analyse=new GmwNewsAnalyse();
		String url="http://news.gmw.cn/newspaper/2013-08/19/content_1974049.htm";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return true;
	}
}
